/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.TimeClockServer;

import TimeRecording.TimeClockServer.messages.ClockMessage;
import java.util.LinkedList;

/**
 * This class is essentially just a linked list of discrete messages to be
 * sent to the clock terminal.  The only additional functionality is that which
 * supports a simple semaphore for recording/testing receipt of an
 * acknowledgement from the terminal.
 *
 * @author david
 */
public class TxBuffer {

    private LinkedList<ClockMessage> list = new LinkedList<ClockMessage>();

    // A simple semaphore indicating that an acknowledgement is to be transmitted
    private boolean txAck = false;

    // A simple semaphore indicating that a time update is to be transmitted
    private boolean txTime = false;

    public synchronized boolean isEmpty() {
        return list.isEmpty();
    }
    
    public synchronized ClockMessage removeMessage() {
        return list.removeFirst();
    }

    public synchronized void addMessage(ClockMessage message) {
        list.add(message);
    }

    /**
     * Sets a simple semaphore to indicate that an acknowledgement is to be
     * transmitted.
     */
    public synchronized void recordAckRequired() {
        txAck = true;
    }

    /**
     * Simply tests the "acknowledgement" semaphore and returns its value.  The
     * semaphore is cleared as part of the method.
     *
     * @return a boolean indicating that an acknowledgement is to be transmitted
     */
    public synchronized boolean testAckRequired() {
        boolean result = txAck;
        txAck = false;
        return result;
    }

    /**
     * Sets a simple semaphore to indicate that an acknowledgement is to be
     * transmitted.
     */
    public synchronized void recordTimeToBeTransmitted() {
        txTime = true;
    }

    /**
     * Simply tests the "acknowledgement" semaphore and returns its value.  The
     * semaphore is cleared as part of the method.
     *
     * @return a boolean indicating that an acknowledgement is to be transmitted
     */
    public synchronized boolean testTimeToBeTransmitted() {
        boolean result = txTime;
        txTime = false;
        return result;
    }
}